package cn.gson.crm.controller.money;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.crm.model.dao.UniversityTuitionDao;
import cn.gson.crm.model.entity.HightSchoolTuition;
import cn.gson.crm.model.entity.UniversityTuition;
import cn.gson.crm.model.mapper.UniversityTuitionMapper;

@Controller
@RequestMapping("/university")
public class UniversityTuitionController {

	@Autowired
	private UniversityTuitionDao universityDao;
	
	@Autowired
	private UniversityTuitionMapper universityMapper;
	
	/**
	 * 查询大学生的学费信息
	 * @param payrecord
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> index(UniversityTuition university, @RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize){
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<UniversityTuition> pageData = universityMapper.findAllUniversityTuition(university);
		 
		PageInfo<UniversityTuition> info = new PageInfo<>(pageData);
		// 组装返回结果
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	/**
	 * 删除高中生学费信息
	 */
	
	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String,Object> delete(@PathVariable("id") Long id) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		UniversityTuition p;
		if (id != null && (p = universityDao.findOne(id)) != null) {
			p.setDeleted(false);
			obj.put("success", true);
		} else {
			obj.put("message", "此记录不存在！");
		}
		return obj;
	}
	
	
	/**
	 * 编辑表单
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping({ "edituniversity", "edituniversity/{id}" })
	public String form(@PathVariable(name = "id", required = false) Long id, Model model) {
		if (id != null) {
			UniversityTuition p = universityDao.findOne(id);
			if (p != null) {		
				model.addAttribute("university", p);
			}
		}	
		List<Map<String,Object>> hight=universityMapper.selectAllStudentName();
		model.addAttribute("hightss",hight);
		
		List<Map<String,Object>> className=universityMapper.selectAllClassName();
		model.addAttribute("classNames",className);
		return "university/edituniversity";
	}

	/**
	 * 保存
	 * @param stuinfor
	 * @param br
	 * @return
	 */
	@PostMapping("saveUniversity")
	@ResponseBody
	@Transactional
	public Map<String,Object> save(@Valid UniversityTuition university, BindingResult br) {
		Map<String,Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("erros", br.getFieldErrors());
		} else {
			if (university.getStudent().getId() == null) {
					obj.put("message", "该用户不存在！");
					return obj;
				
			} else {
				university.setDeleted(true);
				universityDao.save(university);
				obj.put("success", true);
			}	
		}
		return obj;
	}
	
	/**
	 * 导入
	 */
	@RequestMapping("daoru")
	public String daoru(){
			
		return "university/daoru";
	}
	
}
